package com.zmxstudy.gzu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.zmxstudy.gzu.domain.TbSku;
import com.zmxstudy.gzu.domain.TbSpu;
import com.zmxstudy.gzu.mapper.TbBrandMapper;
import com.zmxstudy.gzu.mapper.TbCategoryMapper;
import com.zmxstudy.gzu.mapper.TbSkuMapper;
import com.zmxstudy.gzu.mapper.TbSpuMapper;
import com.zmxstudy.gzu.utils.ProductCodeMsg;
import com.zmxstudy.gzu.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Tag(name = "Sku管理")
@RestController
@RequestMapping(value = "/product-service/skus")
@CrossOrigin
@SuppressWarnings("rawtypes")
public class SkuController {

    @Resource
    TbBrandMapper tbBrandMapper;
    @Resource
    TbSkuMapper tbSkuMapper;
    @Resource
    TbSpuMapper tbSpuMapper;
    @Resource
    TbCategoryMapper tbCategoryMapper;

    @Operation(summary = "新增")
    @PostMapping
    @ResponseBody
    public Result addDesign(@RequestBody TbSku tbSku){

        Date date = new Date();
        tbSku.setCreateTime(date);
        tbSku.setUpdateTime(date);
        tbSkuMapper.insert(tbSku);
        TbSpu tbSpu = tbSpuMapper.selectById(tbSku.getSpuId());
        String str = "0";
        if (str.equals(tbSpu.getIsMarketable())){
            tbSpu.setIsMarketable("1");
            tbSpuMapper.updateById(tbSpu);
        }
        return Result.success();
    }

    @Operation(summary = "修改")
    @PutMapping
    public Result updateSku(@RequestBody TbSku tbSku){

        tbSkuMapper.updateById(tbSku);
        return Result.success();

    }

    @Operation(summary = "查询所有")
    @GetMapping()
    Result<List<TbSku>> findAll(){
        List<TbSku> list = tbSkuMapper.selectList(null);
        return Result.success("查询成功",list);
    }

    @Operation(summary = "根据SpuId查询")
    @GetMapping("/findBySpuId/{id}")
    Result findBySpuId(@PathVariable Long id){

        LambdaQueryWrapper<TbSku> laq = new LambdaQueryWrapper<>();
        laq.eq(TbSku::getSpuId, id);
        List<TbSku> list = tbSkuMapper.selectList(laq);

        return Result.success("查询成功",list);
    }


    @Operation(summary = "根据ID查询")
    @GetMapping("/{id}")
    Result findSkuById(@PathVariable Long id){
        TbSku tbSku = tbSkuMapper.selectById(id);
        if (tbSku == null){
            return Result.error(ProductCodeMsg.ADD_error);
        }
        return Result.success("查询成功",tbSku);
    }

    /**
     * 删除商品
     */

    @Operation(summary = "根据ID删除")
    @DeleteMapping("/{id}")
    public Result deleteSkut(@PathVariable long id){
        TbSku tbSku = tbSkuMapper.selectById(id);
        if (tbSku == null){
            return Result.error(ProductCodeMsg.DELETE_error);
        }

        LambdaQueryWrapper<TbSku> laq=new LambdaQueryWrapper<>();
        laq.eq(TbSku::getSpuId,tbSku.getSpuId());
        List<TbSku> list = tbSkuMapper.selectList(laq);

        if (list.size()==1){
            TbSpu tbSpu = tbSpuMapper.selectById(tbSku.getSpuId());
            tbSpu.setIsMarketable("0");
            tbSpuMapper.updateById(tbSpu);
        }

        tbSkuMapper.deleteById(id);
        return Result.success();
    }




}
